package com.houdoudou.customer.sqlite;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;



public class CarManagement {
    
	private DBHelper dbhelper;
	private SQLiteDatabase db;
	Context context;
	String table="Tb_Selected_Branch";
	public CarManagement(Context context) {
		dbhelper= new DBHelper(context);
		db=dbhelper.getWritableDatabase();
		this.context=context;
	}
	
	public boolean insert(JSONObject jo){
		boolean result=true;
		
		         try{
			           ContentValues cv = new ContentValues();	
			           cv.put("Supplier_Telnum", jo.getString("Supplier_Telnum"));	
			           cv.put("Arrive_Time", jo.getString("Arrive_Time"));
			          
			           cv.put("Product_ID", jo.getInt("Product_ID"));
			           cv.put("Product_Name", jo.getString("Product_Name"));	
			           
			           cv.put("Branch_ID", jo.getInt("Selected_Branch_ID"));	
			           cv.put("Branch_Name", jo.getString("Selected_Branch_Name"));
			           cv.put("Branch_Num", jo.getInt("Selected_Branch_Num"));			           
			           cv.put("Branch_UnitPrice", jo.getDouble("Selected_Branch_UnitPrice"));
			           cv.put("Branch_Weight",jo.getInt("Selected_Branch_Weight"));
			           cv.put("Product_PicDir",jo.getString("Product_PicDir"));
			           
			           long rowid = db.insert("Tb_Selected_Branch", null, cv);			
			           //Toast.makeText(context, "已插入一条测试数据，id =" + rowid, Toast.LENGTH_SHORT).show();
			
		         }catch(Exception e){
		        	 e.printStackTrace();
		        	 result=false;
		         }
		
		return result;
	} 
	

	public boolean updateOneRecord(JSONObject jo){
		boolean result=true;
		
		         try{
			           ContentValues cv = new ContentValues();	
			           cv.put("Supplier_Telnum", jo.getString("Supplier_Telnum"));	
			           cv.put("Product_ID", jo.getInt("Product_ID"));
			           cv.put("Product_Name", jo.getString("Product_Name"));	
			           
			           cv.put("Branch_ID", jo.getInt("Branch_ID"));	
			           cv.put("Branch_Name", jo.getString("Branch_Name"));
			           cv.put("Branch_Num", jo.getInt("Branch_Num"));			           
			           cv.put("Branch_UnitPrice", jo.getDouble("Branch_UnitPrice"));
			           cv.put("Branch_Weight", jo.getInt("Branch_Weight"));
			           cv.put("Arrive_Time", jo.getString("Arrive_Time"));
			           String whereClause = "Branch_ID=? ";//修改条件			           
				    	String[] whereArgs = {String.valueOf(jo.getInt("Branch_ID"))};//修改条件的参数
				    	db.update(table,cv,whereClause,whereArgs);//执行修改		    	
			          	
			           //Toast.makeText(context, "更新，id =" + jo.getInt("Branch_ID"), Toast.LENGTH_SHORT).show();
			
		         }catch(Exception e){
		        	 e.printStackTrace();
		        	 result=false;
		         }
		
		return result;
	} 
	
	public JSONArray  queryByProduct_ID(String Product_ID){
		  //Toast.makeText(context, "查询  Product_ID =" + Product_ID, Toast.LENGTH_SHORT).show();
		JSONArray result= new JSONArray();
		      try{
		 Cursor c = db.rawQuery("SELECT * FROM Tb_Selected_Branch  where Product_ID=?", new String[]{ Product_ID}); 
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put("Supplier_Telnum",  c.getString(c.getColumnIndex("Supplier_Telnum")));
	        	 record.put("Product_ID",  c.getInt(c.getColumnIndex("Product_ID")));
	        	 record.put("Product_Name",  c.getString(c.getColumnIndex("Product_Name")));
	        	 record.put("Branch_ID",  c.getInt(c.getColumnIndex("Branch_ID")));
	        	 record.put("Branch_Name",  c.getString(c.getColumnIndex("Branch_Name")));
	        	 record.put("Branch_Num",  c.getInt(c.getColumnIndex("Branch_Num")));
	        	 record.put("Branch_UnitPrice",  c.getDouble(c.getColumnIndex("Branch_UnitPrice")));	
	        	 record.put("Branch_Weight", c.getInt(c.getColumnIndex("Branch_Weight")));
	        	 record.put("Arrive_Time", c.getString(c.getColumnIndex("Arrive_Time")));
	        	   result.add(record);        	
	        }
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		      //Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 
	
	public JSONArray  queryBySupplier(String Supplier_Telnum,String Arrive_Time ){
		  //Toast.makeText(context, "查询  Supplier_Telnum =" + Supplier_Telnum, Toast.LENGTH_SHORT).show();
		JSONArray result= new JSONArray();
		      try{
		 Cursor c = db.rawQuery("SELECT * FROM Tb_Selected_Branch  where Supplier_Telnum=? and Arrive_Time=?", new String[]{ Supplier_Telnum,Arrive_Time}); 
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put("Supplier_Telnum",  c.getString(c.getColumnIndex("Supplier_Telnum")));
	        	 record.put("Product_ID",  c.getInt(c.getColumnIndex("Product_ID")));
	        	 record.put("Product_Name",  c.getString(c.getColumnIndex("Product_Name")));
	        	 record.put("Branch_ID",  c.getInt(c.getColumnIndex("Branch_ID")));
	        	 record.put("Branch_Name",  c.getString(c.getColumnIndex("Branch_Name")));
	        	 record.put("Branch_Num",  c.getInt(c.getColumnIndex("Branch_Num")));
	        	 record.put("Branch_UnitPrice",  c.getDouble(c.getColumnIndex("Branch_UnitPrice")));	
	        	 record.put("Branch_Weight", c.getInt(c.getColumnIndex("Branch_Weight")));
	        	 record.put("Arrive_Time", c.getString(c.getColumnIndex("Arrive_Time")));
	        	 record.put("Product_PicDir", c.getString(c.getColumnIndex("Product_PicDir")));
	        	 record.put("Is_Selected",false);
	        	   result.add(record);        	
	        }
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		      //Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 
	
	public boolean deleteOneRecord(JSONObject branch){
		 boolean result=false;
		   try{
			   
			   String whereClause="Supplier_Telnum=? and Product_ID=? and Branch_ID=?";
			   String [] whereArgs= new String[3];
			   whereArgs[0]=branch.getString("Supplier_Telnum");
			   whereArgs[1]=String.valueOf(branch.getInt("Product_ID"));
			   whereArgs[2]=String.valueOf(branch.getInt("Branch_ID"));
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   db.delete("Tb_Selected_Branch", whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   //Toast.makeText(context, "删除单条记录" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	
	public boolean deleteOneProduct(JSONObject branch){
		boolean result=false;
		   try{
			   
			   String whereClause="Supplier_Telnum=? and Product_ID=? ";
			   String [] whereArgs= new String[2];
			   whereArgs[0]=branch.getString("Supplier_Telnum");
			   whereArgs[1]=String.valueOf(branch.getInt("Product_ID"));
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   db.delete("Tb_Selected_Branch", whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   //Toast.makeText(context, "删除产品" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	public boolean deleteOneSupplier(JSONObject branch){
		boolean result=false;
		   try{
			   
			   String whereClause="Supplier_Telnum=? and Arrive_Time=? ";
			   String [] whereArgs= new String[2];
			   whereArgs[0]=branch.getString("Supplier_Telnum");
			   whereArgs[1]=branch.getString("Arrive_Time");
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   db.delete("Tb_Selected_Branch", whereClause, whereArgs);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   //Toast.makeText(context, "删除一个店所有购物车内东西" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	public boolean deleteAll(){
		boolean result=false;
		   try{
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   db.delete("Tb_Selected_Branch", null, null);
			   result=true;
		   }catch( Exception e){
			  e.printStackTrace();
			  result=false;
		   }finally{ 
			   //Toast.makeText(context, "删除一个店所有购物车内东西" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return result;
	}
	
	public JSONObject queryOneBranch(JSONObject branch){
		JSONObject record= new JSONObject();
		   try{
			   
			   String baseClause="SELECT * FROM Tb_Selected_Branch where";
			   String whereClause=" Supplier_Telnum=? and Product_ID=? and Branch_ID=? ";
			   String [] whereArgs= new String[3];
			   whereArgs[0]=branch.getString("Supplier_Telnum");
			   whereArgs[1]=String.valueOf(branch.getInt("Product_ID"));
			   whereArgs[2]=String.valueOf(branch.getInt("Selected_Branch_ID"));
			  // db.execSQL("delete from Tb_Selected_Branch where Tb_Selected_Branch=?",params);
			   Cursor c=  db.rawQuery(baseClause+whereClause, whereArgs);
			   if(c.moveToNext()){
				     
				     record.put("Action_Status",true);
		        	 record.put("Supplier_Telnum",  c.getString(c.getColumnIndex("Supplier_Telnum")));
		        	 record.put("Product_ID",  c.getInt(c.getColumnIndex("Product_ID")));
		        	 record.put("Product_Name",  c.getString(c.getColumnIndex("Product_Name")));
		        	 record.put("Branch_ID",  c.getInt(c.getColumnIndex("Branch_ID")));
		        	 record.put("Branch_Name",  c.getString(c.getColumnIndex("Branch_Name")));
		        	 record.put("Branch_Num",  c.getInt(c.getColumnIndex("Branch_Num")));
		        	 record.put("Branch_UnitPrice",  c.getDouble(c.getColumnIndex("Branch_UnitPrice")));
		        	 record.put("Branch_Weight", c.getInt(c.getColumnIndex("Branch_Weight")));
			   }else{
				   record.put("Action_Status",false);
			   }
		   }catch( Exception e){
			  e.printStackTrace();
			  record.put("Action_Status",false);
			 
		   }finally{ 
			   //Toast.makeText(context, "查询单条记录" +branch.toString(), Toast.LENGTH_SHORT).show();
		   }
		  
		   return record;
	}
	
	public JSONArray  queryAll( ){
		  //Toast.makeText(context, "查询  所有", Toast.LENGTH_SHORT).show();
		JSONArray result= new JSONArray();
		      try{
		 Cursor c = db.rawQuery("SELECT * FROM Tb_Selected_Branch", null); 
	        while (c.moveToNext()) {  
	        	 JSONObject record= new JSONObject();
	        	 record.put("Supplier_Telnum",  c.getString(c.getColumnIndex("Supplier_Telnum")));
	        	 record.put("Product_ID",  c.getInt(c.getColumnIndex("Product_ID")));
	        	 record.put("Product_Name",  c.getString(c.getColumnIndex("Product_Name")));
	        	 record.put("Branch_ID",  c.getInt(c.getColumnIndex("Branch_ID")));
	        	 record.put("Branch_Name",  c.getString(c.getColumnIndex("Branch_Name")));
	        	 record.put("Branch_Num",  c.getInt(c.getColumnIndex("Branch_Num")));
	        	 record.put("Branch_UnitPrice",  c.getDouble(c.getColumnIndex("Branch_UnitPrice")));	
	        	 record.put("Branch_Weight", c.getInt(c.getColumnIndex("Branch_Weight")));
	        	 record.put("Arrive_Time", c.getString(c.getColumnIndex("Arrive_Time")));
	        	
	        	   result.add(record);        	
	        }
	        c.close();  
		      }catch(Exception e){
		    	  e.printStackTrace();
		      }
		      //Toast.makeText(context, "查询产品" +result.toString(), Toast.LENGTH_SHORT).show();
		return result;		
	} 
	
	
	
}
